<?php
// +-------------------------------------------------------------------
// |
// +-------------------------------------------------------------------
// | Copyright (c) 2009-2016 All rights reserved.
// +-------------------------------------------------------------------
namespace Service\Api\V1;

class ServiceOrderList
{
    public $login = true; //是否需要登录才能访问该接口
    public $title = "缴纳记录列表ok";
    public $group = '物业缴费';
    public $desc = "";

    public $input = [
        'page_num' => 'int;label=分页;comment=默认为1',
        'page_size' => 'int;label=分页条数;comment=默认为5',
    ];

    public $output = [
        'page_num' => 'label=当前页码;',
        'page_size' => 'label=每页条数;',
        'page_count' => 'label=总条数;',
        'page_total' => 'label=总页数;',
        'list' => [
            [
                'id' => 'label=订单id;',
                'order_no' => 'label=订单号;',
                'building_name' => 'label=楼栋号;',
                'house_name' => 'label=房间号;',
                'real_name' => 'label=真实姓名;',
                'mobile' => 'label=手机号;',
                'buy_num' => 'label=购买数量;',
                'total_money' => 'label=应付金额;',
                'pay_money' => 'label=实付金额;',
                'pay_type' => 'label=支付方式1微信支付 2支付宝支付;',
                'pay_time' => 'label=支付时间;',
                'order_status' => 'label=订单状态0已提交1已支付;',
                'order_type' => 'label=订单类型：0房租缴纳 1物业费缴纳 2水费缴纳 3电费缴纳 4保证金缴纳;',
            ]
        ]
    ];

    public function run($param, $uid, $login_info)
    {
        $page_num = $param['page_num'] ?: 1;
        $page_size = $param['page_size'] ?: 10;

        $list = M('order')
            ->where([
                'uid' => $uid,
                'order_status' => 1,
                'order_type' => ['in', '0,1'],
            ])
            ->order('id desc')->select();
        foreach($list as &$item){
            $order_detail = M('order_detail')->where(['order_id' => $item['id']])->find();
            $building = M('building')->where(['id' => $order_detail['building_id']])->find();
            $house = M('house')->where(['id' => $order_detail['house_id']])->find();
            $item['building_name'] = $building['name'];
            $item['house_name'] = $house['name'];
            $item['real_name'] = $login_info['real_name'];
            $item['mobile'] = $login_info['mobile'];
        }

        $list_margin = M('margin_order')
            ->where([
                'uid' => $uid,
                'order_status' => 1,
            ])
            ->order('id desc')
            ->select();
        foreach($list_margin as &$item){

            $building = M('building')->where(['id' => $item['building_id']])->find();
            $house = M('house')->where(['id' => $item['house_id']])->find();
            $item['building_name'] = $building['name'];
            $item['house_name'] = $house['name'];
            $item['real_name'] = $login_info['real_name'];
            $item['mobile'] = $login_info['mobile'];
            $item['order_type'] = 4;
            $list[] = $item;
        }


        krsort($list, 'pay_time');

        return [
            'list'=> $list,
            'page_count' => 0,
            'page_total' => 0
        ];
    }
}
